// 可持久化平衡树
// https://www.luogu.com.cn/problem/P3835
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
using ll = long long;
using T = int;
T rad(); // quick read
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
const int max_size = 5 + 100;
const int maxn = 5 + 8e5;

class Treap {
  private:
    int tot;
    int val[maxn << 5], pri[maxn << 5];
    int siz[maxn << 5];
    int ch[maxn << 5][2];

    int new_node(int x) {
        return val[++tot] = x, pri[tot] = rand(), siz[tot] = 1, ch[tot][0] = 0, ch[tot][1] = 0, tot;
    }
    void maintain(int u) { siz[u] = siz[ch[u][0]] + siz[ch[u][1]] + 1; }
    int cp(int u) {
        return val[++tot] = val[u], pri[tot] = pri[u], siz[tot] = siz[u], ch[tot][0] = ch[u][0],
               ch[tot][1] = ch[u][1], tot;
    }

    void split(int u, int key, int &l, int &r) { // 按 key 拆分 u 子树，l <= key，r > key
        if (u == 0) {
            l = r = 0;
            return;
        }
        u = cp(u);
        if (val[u] <= key) // kl <= k, kr > k
            l = u, split(ch[u][1], key, ch[u][1], r);
        else
            split(ch[u][0], key, l, ch[u][0]), r = u;
        maintain(u);
    }

    int merge(int u, int v) {
        if (u == 0 || v == 0)
            return u ^ v;
        if (pri[u] > pri[v]) {
            u = cp(u);
            ch[u][1] = merge(ch[u][1], v), maintain(u);
            return u;
        } else {
            v = cp(v);
            ch[v][0] = merge(u, ch[v][0]), maintain(v);
            return v;
        }
    }

  public:
    int insert(int root, int x) {
        int l, r, u = new_node(x);
        split(root, x, l, r);
        return merge(merge(l, u), r);
    }

    int erase(int root, int x) {
        int l, mid, r;
        split(root, x, l, r);
        split(l, x - 1, l, mid);
        return merge(merge(l, merge(ch[mid][0], ch[mid][1])), r);
    }

    int get_rank(int u, int x) { // 返回小于 x 的节点数量
        int lsiz = 0;
        while (u > 0) {
            if (x > val[u]) lsiz += siz[ch[u][0]] + 1;
            u = ch[u][x > val[u]];
        }
        return lsiz;
    }

    int get_val(int root, int rk) { // 返回排名为 rk 的节点值
        int u = root;
        while (u > 0) {
            int now_rk = siz[ch[u][0]] + 1;
            if (rk == now_rk)
                return val[u];
            u = ch[u][rk > now_rk];
            if (rk > now_rk)
                rk -= now_rk;
        }
        return 0;
    }

    int get_pre(int root, int x) {
        int u = root, ret = -inf;
        while (u > 0) {
            if (x > val[u])
                ret = max(ret, val[u]);
            u = ch[u][x > val[u]];
        }
        return ret;
    }

    int get_nex(int root, int x) {
        int u = root, ret = inf;
        while (u > 0) {
            if (x < val[u])
                ret = min(ret, val[u]);
            u = ch[u][x >= val[u]];
        }
        return ret;
    }

    void show(int p, int dep = 1) {
        if (p == 0)
            return;
        show(ch[p][0], dep + 1);
        printf("|| val: %d, deep: %d, siz: %d, p: %d\n", val[p], dep, siz[p], p);
        show(ch[p][1], dep + 1);
    }
} tp;

int root[maxn << 2], rsiz;

int main() {
    // freopen("main.in", "r", stdin);
    int n = rad();
    rf(i, n) {
        int ver = rad(), op = rad();
        root[i] = root[ver];
        if (op == 1) {
            int tmp = rad();
            root[i] = tp.insert(root[ver], tmp);
        } else if (op == 2) {
            int tmp = rad();
            root[i] = tp.erase(root[ver], tmp);
        } else if (op == 3) {
            int tmp = rad();
            printf("%d\n", tp.get_rank(root[ver], tmp) + 1);
        } else if (op == 4) {
            int tmp = rad();
            printf("%d\n", tp.get_val(root[ver], tmp));
        } else if (op == 5) {
            int tmp = rad();
            printf("%d\n", tp.get_pre(root[ver], tmp));
        } else if (op == 6) {
            int tmp = rad();
            printf("%d\n", tp.get_nex(root[ver], tmp));
        } else {
            tp.show(root[ver]);
            puts("");
        }
    }
}

T rad() {
    T back = 0;
    int ch = 0, posi = 0;
    for (; ch < '0' || ch > '9'; ch = getchar()) posi = ch ^ '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar()) back = (back << 1) + (back << 3) + (ch & 0xf);
    return posi ? back : -back;
}